'use strict';
const fs = require('fs'); 

const ERROR = require('../config/defaultErrorInfo').defaultErrorInfo;
const wxFileSrc = require('../config/defaultWxFileSrc').wxFileSrc; 
const _fileSystem = require('../common/fileSystem');
const FILE_TYPE = 'json';

class BuildWxApp {
        constructor (fileName = ''){
                  fileName = fileName.replace(/\\/g,"/");
                  this.file =  {
                          path : fileName,
                          type : fileName.split(".").reverse()[0],
                          name : fileName.split("/").reverse()[0].split('.')[0] ,
                          root : fileName.replace(fileName.split("/").reverse()[0], "")
                  };
                  if ( this.file.type !== FILE_TYPE ) throw(ERROR[0]);
                  this.appConfig = JSON.parse(fs.readFileSync(this.file.path, "utf-8"));
                  if ( !this.appConfig.pages ) throw(error[1]); 

                  void(this._bulidAppMain());
                  void(this._bulidAppPages());
        }

        _bulidAppMain () {
                  for (let key in wxFileSrc.wxAppContent) {
                          let fileName = this.file.root + this.file.name + key; 
                          if (fs.existsSync(fileName)) {
                               console.log(`INFO: mkfile '${fileName}'\tfile already exist`);
                          } else {
                              void(fs.writeFileSync(fileName, wxFileSrc.wxAppContent[key]));
                              if (fs.existsSync(fileName)) {
                                     console.log(`INFO: mkfile '${fileName}' \tsuccess`);
                              } else {
                                     console.log(`INFO: mkfile '${fileName}' \terror`);
                              }
                          }
                  }
        }

        _bulidAppPages () {
                  this.appConfig.pages.forEach((dirName) => {
                          let newDirName = dirName.split("/");
                          void(newDirName.pop());
                          newDirName = this.file.root + newDirName.join("/");  
                          console.log(`\ninfo: mkdir '${newDirName}' is ` + _fileSystem.mkdirsSync(newDirName));
                          for (let key in wxFileSrc.wxPageContent) {
                                  let fileName = this.file.root + dirName + key;
                                  if ( fs.existsSync(fileName)) {
                                       console.log(`\tINFO: mkfile '${fileName}' \tfile already exist`);
                                  }else{
                                      void(fs.writeFileSync(fileName, wxFileSrc.wxPageContent[key]));
                                      if (fs.existsSync(fileName)) {
                                              console.log(`\tINFO: mkfile '${fileName}' \tsuccess`);
                                      } else {
                                              console.log(`\tINFO: mkfile '${fileName}' \terror`);
                                      }
                                  }
                          }
                  });
        }

}

module.exports = { 
        BuildWxApp 
};